package com.example.myweather.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView

import com.example.myweather.R
import com.example.myweather.bean.WeatherApi

import java.util.Calendar
import java.util.TimeZone

class Min_precipitation_adapter(private val mweatherApi: WeatherApi) : RecyclerView.Adapter<Min_precipitation_adapter.ViewHolder>() {

    private var mContext: Context? = null

    internal class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var cardView: CardView
        var time: TextView
        var value: TextView

        init {
            cardView = view as CardView
            time = itemView.findViewById<View>(R.id.result_min_probalility_time) as TextView
            value = itemView.findViewById<View>(R.id.result_min_probalility_value) as TextView
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        if (mContext == null) {
            mContext = parent.context
        }
        val view = LayoutInflater.from(parent.context).inflate(R.layout.card_view_min, parent, false)
        val holder = Min_precipitation_adapter.ViewHolder(view)
        holder.cardView.setOnClickListener {
            val position = holder.adapterPosition
            Toast.makeText(mContext, position.toString(), Toast.LENGTH_SHORT).show()
            //                    Count count=mCountList.get(position);
            //                Intent intent=new Intent(mContext,Exam2tActivity.class);
            //                intent.putExtra(Exam2tActivity.EXAMEXAM,exam.getExamId());
            //                intent.putExtra(Exam2tActivity.EXAMCOUNT,String.valueOf(i));
            //                intent.putExtra(Exam2tActivity.EXAMNAME,exam.getName());
            //                mContext.startActivity(intent);
        }
        return holder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        val calendar = Calendar.getInstance()


        calendar.timeZone = TimeZone.getTimeZone("GMT+8:00")

        //小时
        val hour = calendar.get(Calendar.HOUR_OF_DAY)
        //分钟
        val minute = calendar.get(Calendar.MINUTE)


        holder.time.text = getTimes(hour, minute, position)

        //holder.time.setText(String.valueOf(position));

        holder.value.text = mweatherApi.result.minutely.precipitation[position].toString()


    }


    override fun getItemCount(): Int {
        return mweatherApi.result.minutely.precipitation.size
    }

    fun getTimes(hours: Int, mi: Int, p: Int): String {
        var hours = hours
        var mi = mi
        if (mi + p >= 60) {

            mi = mi + p - 60

            if (hours + 1 == 24) {
                hours = hours - 23
            } else {
                hours = hours + 1
            }

            return "$hours:$mi"

        } else {
            mi = mi + p
            return "$hours:$mi"
        }


    }


}
